import 'package:flutter/material.dart';
import 'package:test_flutter/dio/Api.dart';
import 'package:test_flutter/dio/DioUtil.dart';
import 'package:test_flutter/utils/ToastUtil.dart';
import 'package:test_flutter/utils/factory/Fit.dart';

/**
 * 消费记录
 */

class ConsumptionPage extends StatefulWidget {
  final Map<String, dynamic> bean;
  ConsumptionPage(this.bean);
  @override
  ConsumptionState createState() => new ConsumptionState();
}

class ConsumptionState extends State<ConsumptionPage>  {
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
  bool _saving = false;
  bool hasInit =false;
  var list = new List();


  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if(!hasInit){
      _getCardConsumeLogs();
    }

    hasInit = true;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _pullToRefresh, // onRefresh 参数是一个 Future<Null> 的回调
        child: ListView.builder(
          padding: EdgeInsets.only(
              left: Fit.scaleX(30),
              top: Fit.scaleY(40),
              right: Fit.scaleX(30),
              bottom: Fit.scaleY(40)),
          itemCount: list.length,
          itemBuilder: (BuildContext context, int position) {
            return  getItem(position);
          },
        ),
      ),
    );
  }


  getItem(int position) {
    Map<String, dynamic> bean = list[position];
    var row = GestureDetector(
      onTap: () {
        /*Navigator.push<Object>(context,
            new MaterialPageRoute(builder: (BuildContext context) {
              return new HistoryPage(bean);
            }));*/
      },
      child: new Container(
        padding: EdgeInsets.only(left: Fit.scaleX(30)),
        width: MediaQuery.of(context).size.width,
        height: Fit.scaleY(400),
        decoration: BoxDecoration(
          gradient: LinearGradient(colors: [Color(0xFBEBEBE), Color(0xFFEE7942)]),
          borderRadius: BorderRadius.circular(Fit.scaleX(15)),
        ),
        child: Row(
          children: <Widget>[
            Container(
              width: Fit.scaleX(300),
              height: Fit.scaleY(400),
              child: Center(
                child:  new CircleAvatar(
                  backgroundColor: Color(0xAAEED5D2),
                  radius: 72.0,
                  child: new Text(
                    bean['type'],
                    style: new TextStyle(color: Color(0xFFFFFFFF), fontSize: 20.0),
                  ),
                ),
              ),
            ),
            Container(
              child: Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(
                        left: Fit.scaleX(10),
                        top: Fit.scaleX(80)),
                    width: Fit.scaleX(650),
                    child: Text(
                      "店铺"+': ${bean['shopname']}',
                      textAlign: TextAlign.left,
                      style: new TextStyle(
                          inherit: false,
                          color: Color(0xFFFFFFFF),
                          fontWeight:FontWeight.w500,
                          letterSpacing:6.0,
                          fontSize: Fit.scaleTextSize(40),
                          fontStyle: FontStyle.italic),
                    ),
                  ),

                  Container(
                    padding: EdgeInsets.only(
                        left: Fit.scaleX(10),
                        top: Fit.scaleX(40)),
                    width: Fit.scaleX(650),
                    child: Text(
                      "消费"+': ${bean['amtdues']}',
                      textAlign: TextAlign.left,
                      style: new TextStyle(
                          inherit: false,
                          color: Color(0xFFFFFFFF),
                          fontWeight:FontWeight.w500,
                          letterSpacing:6.0,
                          fontSize: Fit.scaleTextSize(40),
                          fontStyle: FontStyle.italic),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(
                        left: Fit.scaleX(10),
                        top: Fit.scaleX(40)),
                    width: Fit.scaleX(650),
                    child: Text(
                      "时间"+': ${bean['createdate']}',
                      textAlign: TextAlign.left,
                      style: new TextStyle(
                          inherit: false,
                          color: Color(0xFFFFFFFF),
                          fontWeight:FontWeight.w500,

                          fontSize: Fit.scaleTextSize(40),
                          fontStyle: FontStyle.italic),
                    ),
                  )


                ],
              ),
            ),
          ],
        ),
      ),
    );
    return Card(
      elevation: Fit.scaleX(20),
      child: row,
    );
  }

  Future<Null> _pullToRefresh() async {
    _getCardConsumeLogs();
    return null;
  }
  ///获取会员卡消费记录
  _getCardConsumeLogs() async {
    setState(() {
      _saving = true;
    });
    Map<String, String> requestMap = new Map();
    requestMap['no'] = widget.bean['CardNo'];

    DioUtil.getInstance(context).get(Api.getCardConsumeLogs, (response) {
      if (response.data['code'] == 1) {
        list = response.data['data']['list'];

        setState(() {});
      } else {
        ToastUtil.showToast(response.data['msg']);
      }

      setState(() {
        _saving = false;
      });
    },
        data: requestMap,
        errorCallBack: (errorMsg) {
      print("error=========================:" + errorMsg);
      ToastUtil.showToast(errorMsg);
      setState(() {
        _saving = false;
      });
    });
  }
}


